package com.lepinwenhua.roomdemo;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import android.content.Context;

/**
 * @author: 魏从响
 * @date: 2025/10/27
 * @class: AppDatabase
 */
@Database(entities = {Fruit.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {

    public abstract FruitDao fruitDao();

    private static volatile AppDatabase instance;

    // 单例模式，确保只有一个数据库实例
    public static synchronized AppDatabase getInstance(Context context) {
        if (instance == null) {
            instance = Room.databaseBuilder(context.getApplicationContext(),
                            AppDatabase.class, "fruit_database")
                    .fallbackToDestructiveMigration() // 数据库版本升级时删除旧数据
                    .build();
        }
        return instance;
    }
}

